package com.zwxu.stack;

import java.util.ArrayList;
import java.util.Stack;

public class SimplifyPathDemo {

	public static void main(String[] args) {
		System.out.print(simplifyPath("/.."));

	}
	
	public static String simplifyPath(String path) {
		Stack<String> paths = new Stack<String>();
		String result = "/";
        String[] arr = path.split("/");
        for (String s : arr)
        {
            if(s.equals(".."))
            {
                if(paths.size() > 0)
                {
                    //paths.remove(paths.size() - 1);
                	paths.pop();
                }
            }
            else if (!s.equals(".") && !s.equals(""))
            {
                //paths.add(s);
            	paths.push(s);
            }
        }
        for (String s : paths)
        {
            result += s + "/";
        }
        if (result.length() > 1)
        {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

}
